﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;

namespace NamazVakti
{
    public partial class MainForm : Form
    {
        delegate void UpdateDelegate();
        DateNavigator navigator;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            object obj = DateTime.Now;
            ThreadPool.QueueUserWorkItem(new WaitCallback(LoadTakvim));
        }

        void LoadTakvim(Object t)
        {
            NamazVakti.CurrentTakvim = XmlUtil.Deserialize(typeof(Takvim)) as Takvim;
            if (NamazVakti.CurrentTakvim == null)
                NamazVakti.CurrentTakvim = new Takvim();

            linkSelectedCity.BeginInvoke(new UpdateDelegate(delegate() { linkSelectedCity.Text = Parameters.SelectedCity; }));

            NamazVakti.CurrentTakvim.Changed += new Takvim.ChangedDelegate(CurrentTakvim_Changed);

            navigator = new DateNavigator();
            navigator.DateChanged += new DateNavigator.DateChangedEvent(navigator_DateChanged);
            navigator.Today();
        }

        void navigator_DateChanged()
        {
            listTakvim.BeginInvoke(new UpdateDelegate(Yenile));
        }

        void CurrentTakvim_Changed()
        {
            listTakvim.BeginInvoke(new UpdateDelegate(Yenile));
            ThreadPool.QueueUserWorkItem(new WaitCallback(SaveTakvim));
        }

        void SaveTakvim(object t)
        {
            XmlUtil.Serialize(NamazVakti.CurrentTakvim);
        }

        void Yenile()
        {
            if (navigator.Current == null) return;

            Vakit current = navigator.Current.GetCurrentVakit();

            listTakvim.Items[0].SubItems[1].Text = navigator.Current.Imsak;
            listTakvim.Items[0].BackColor = current.GetBackColor(VakitEnumeration.Imsak);
            listTakvim.Items[0].ForeColor = current.GetForeColor(VakitEnumeration.Imsak);

            listTakvim.Items[1].SubItems[1].Text = navigator.Current.Gunes;
            listTakvim.Items[1].BackColor = current.GetBackColor(VakitEnumeration.Gunes);
            listTakvim.Items[1].ForeColor = current.GetForeColor(VakitEnumeration.Gunes);

            listTakvim.Items[2].SubItems[1].Text = navigator.Current.Ogle;
            listTakvim.Items[2].BackColor = current.GetBackColor(VakitEnumeration.Ogle);
            listTakvim.Items[2].ForeColor = current.GetForeColor(VakitEnumeration.Ogle);

            listTakvim.Items[3].SubItems[1].Text = navigator.Current.Ikindi;
            listTakvim.Items[3].BackColor = current.GetBackColor(VakitEnumeration.Ikinki);
            listTakvim.Items[3].ForeColor = current.GetForeColor(VakitEnumeration.Ikinki);

            listTakvim.Items[4].SubItems[1].Text = navigator.Current.Aksam;
            listTakvim.Items[4].BackColor = current.GetBackColor(VakitEnumeration.Aksam);
            listTakvim.Items[4].ForeColor = current.GetForeColor(VakitEnumeration.Aksam);

            listTakvim.Items[5].SubItems[1].Text = navigator.Current.Yatsi;
            listTakvim.Items[5].BackColor = current.GetBackColor(VakitEnumeration.Yatsi);
            listTakvim.Items[5].ForeColor = current.GetForeColor(VakitEnumeration.Yatsi);

            labelCurrentDate.Text = navigator.Current.Date.ToShortDateString();
            labelRemaining.WriteRemainingTime(current.Interval, navigator.Current.GetRemainingTime());
        }

        void ChangeCity()
        {
            ChangeCityForm.Open();
            ThreadPool.QueueUserWorkItem(new WaitCallback(LoadTakvim));
        }

        private void menuItemUpdate_Click(object sender, EventArgs e)
        {
            NamazVakti.CurrentTakvim.Update();
        }

        private void buttonPrevious_Click(object sender, EventArgs e)
        {
            navigator.Previous();
        }

        private void buttonToday_Click(object sender, EventArgs e)
        {
            navigator.Today();
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            navigator.Next();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void linkLabel1_Click(object sender, EventArgs e)
        {
            ChangeCity();
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            AboutForm.ShowForm();
        }

        private void menuItemPray_Click(object sender, EventArgs e)
        {
            PrayForm.ShowForm();
        }

    }
}